package org.dayatang.querychannel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;
/**
 * @ClassName: jdbcTemplateObjObj  
 * @Description ：重写   jdbcTemplateObj的queryForObject方法 
 * @Date: 2017年11月22日 上午10:35:17  
 * @Author LiuHongJun 
 * @Version 3.0.0 DHC-DRGs
 */
public class JdbcTemplateDrgs extends JdbcTemplate {
	private static final Logger logger = LoggerFactory.getLogger(JdbcTemplateDrgs.class);
	
	public <T> T queryForObject(String sql, RowMapper<T> rowMapper) throws DataAccessException {
		List<T> results=null;
		try {
			results = query(sql, rowMapper);
			return ObjDataAccessUtils.requiredSingleResult(results);
		}catch(Exception e) {
			e.printStackTrace();
			logger.error("sql错误信息"+e.getMessage()+"\n 报错SQL:"+sql);
			return null;
		}
	}

	public <T> T queryForObject(String sql, Class<T> requiredType) throws DataAccessException {
		return queryForObject(sql, getSingleColumnRowMapper(requiredType));
	}
	public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
			throws DataAccessException {
		List<T> results=null;
		try {
			 results = query(sql, args, argTypes, new RowMapperResultSetExtractor<T>(rowMapper, 1));
			return ObjDataAccessUtils.requiredSingleResult(results);
		}catch(Exception e) {
			e.printStackTrace();
			logger.error("sql错误信息"+e.getMessage()+"\n 报错SQL:"+sql);
			return null;
		}
	}

	public <T> T queryForObject(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException {
		List<T> results=null;
		try {
			results = query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper, 1));
			return ObjDataAccessUtils.requiredSingleResult(results);
		}catch(Exception e) {
			e.printStackTrace();
			logger.error("sql错误信息"+e.getMessage()+"\n 报错SQL:"+sql);
			return null;
		}
	}

	public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException {	
		List<T> results=null;
		try {
			results = query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper, 1));
		}catch(Exception e) {
			e.printStackTrace();
			logger.error("sql错误信息"+e.getMessage()+"\n 报错SQL:"+sql);
			return ObjDataAccessUtils.requiredSingleResult(results);
		}
		return null;
	}

	public <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> requiredType)
			throws DataAccessException {

		return queryForObject(sql, args, argTypes, getSingleColumnRowMapper(requiredType));
	}

	public <T> T queryForObject(String sql, Object[] args, Class<T> requiredType) throws DataAccessException {
		return queryForObject(sql, args, getSingleColumnRowMapper(requiredType));
	}

	public <T> T queryForObject(String sql, Class<T> requiredType, Object... args) throws DataAccessException {
		return queryForObject(sql, args, getSingleColumnRowMapper(requiredType));
	}
	
	public List<Map<String, Object>> queryForList(String sql) throws DataAccessException {
		List<Map<String, Object>> results=null;
		try {
			results = query(sql, getColumnMapRowMapper());
			return results;
		}catch(Exception e) {
			e.printStackTrace();
			logger.error("sql错误信息"+e.getMessage()+"\n 报错SQL:"+sql);
			return results;
		}
	}
	/**
	 *  @Discription：jdbc分页
	 *  @Author： liuhongjun
	 *  @param sql 执行的sql
	 *  @param pageIndex 当前第几页，从0计数
	 *  @param pageSize 每页显示多少条
	 *  @return 返回map
	 *  @throws DataAccessException
	 *  @Date:2018年8月3日上午10:25:04
	 */
	public Map<String, Object> queryForPage(String sql,int pageIndex,int pageSize) throws DataAccessException {
		Map<String, Object> map=new HashMap<String, Object>();
		int start=pageIndex*pageSize;
		String sqlCnt="SELECT count(1) FROM ( ";
		sqlCnt+=sql;
		sqlCnt+=" ) a";
		String pageSqlHead = "SELECT * FROM (SELECT PAGERESULT.*, ROWNUM RN FROM ( \n";        
		String pageSqlFoot =  " \n ) PAGERESULT ) WHERE RN > "+start+" AND RN <= "+(start+pageSize);
		try {
			int  totalCount=queryForObject(sqlCnt, getSingleColumnRowMapper(Integer.class));
			List<Map<String, Object>> list=query(pageSqlHead+sql+pageSqlFoot, getColumnMapRowMapper());
			map.put("data", list);
			map.put("start", start);
			map.put("pageSize", pageSize);
			map.put("resultCount", totalCount);
			map.put("pageCount", totalCount/pageSize);
			map.put("pageIndex", pageIndex);
			return map ;
		}catch(Exception e) {
			e.printStackTrace();
			logger.error("sql错误信息"+e.getMessage()+"\n 报错SQL:"+pageSqlHead+sql+pageSqlFoot);
			return map ;
		}
	}

}

